package com.atguigu.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.app.func.DorisSinkFunction;
import com.atguigu.edu.realtime.app.func.MyAsyncFunction;
import com.atguigu.edu.realtime.beans.TrafficPageViewBean;
import com.atguigu.edu.realtime.beans.TrafficSessionPageViewBean;
import com.atguigu.edu.realtime.utils.DateFormatUtil;
import com.atguigu.edu.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.concurrent.TimeUnit;

//浏览域各来源各访客类别会话粒度页面浏览各窗口汇总表
//各会话页面数

public class DwsTrafficSourceIsNewSessionViewWindow {
    public static void main(String[] args) {
        //基本环境准备
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(5);
        //检查点配置
        /*env.enableCheckpointing(5000l);
        env.getCheckpointConfig().setCheckpointTimeout(60000l);
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000l);
        env.setRestartStrategy(RestartStrategies.failureRateRestart(3, Time.days(30),Time.seconds(10)));
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/edu/ck");
        System.setProperty("HADOOP_USER_NAME","atguigu");*/
        //读取kafka的dwd_traffic_page_log主题
        DataStreamSource<String> jsonStrDs = env.fromSource(MyKafkaUtil.getKafkaSource("dwd_traffic_page_log", "dws_traffic_Session_group"),
                WatermarkStrategy.noWatermarks(), "kafkassorce");

        //转换数据类型
        SingleOutputStreamOperator<JSONObject> jsonObj = jsonStrDs.map(JSON::parseObject);
        //封装bean
        SingleOutputStreamOperator<TrafficSessionPageViewBean> beanDs = jsonObj.map(
                new MapFunction<JSONObject, TrafficSessionPageViewBean>() {
                    @Override
                    public TrafficSessionPageViewBean map(JSONObject jsonObj) throws Exception {
                        JSONObject common = jsonObj.getJSONObject("common");
                        String sc = common.getString("sc");
                        String isNew = common.getString("is_new");
                        String sid = common.getString("sid");
                        Long ts = jsonObj.getLong("ts");

                        return new TrafficSessionPageViewBean(
                                "",
                                "",
                                sc,
                                "",
                                isNew,
                                "",
                                sid,
                                1l,
                                ts
                        );
                    }
                }
        );

        //设定水位线
        SingleOutputStreamOperator<TrafficSessionPageViewBean> watermarkDs = beanDs.assignTimestampsAndWatermarks(
                WatermarkStrategy.<TrafficSessionPageViewBean>forMonotonousTimestamps()
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<TrafficSessionPageViewBean>() {
                                    @Override
                                    public long extractTimestamp(TrafficSessionPageViewBean element, long recordTimestamp) {
                                        return element.getTs();
                                    }
                                }
                        )
        );

        //分组开窗聚合
        SingleOutputStreamOperator<TrafficSessionPageViewBean> reduceDs = watermarkDs.keyBy(
                new KeySelector<TrafficSessionPageViewBean, Tuple3<String, String, String>>() {
                    @Override
                    public Tuple3<String, String, String> getKey(TrafficSessionPageViewBean value) throws Exception {
                        return Tuple3.of(value.getSc(), value.getIsNew(), value.getSid());
                    }
                }
        ).window(
                TumblingEventTimeWindows.of(Time.seconds(10))
        ).reduce(
                new ReduceFunction<TrafficSessionPageViewBean>() {
                    @Override
                    public TrafficSessionPageViewBean reduce(TrafficSessionPageViewBean value1, TrafficSessionPageViewBean value2) throws Exception {
                        value1.setPvCt(value1.getPvCt() + value2.getPvCt());
                        return value1;
                    }
                },
                new ProcessWindowFunction<TrafficSessionPageViewBean, TrafficSessionPageViewBean, Tuple3<String, String, String>, TimeWindow>() {
                    @Override
                    public void process(Tuple3<String, String, String> stringStringStringTuple3, ProcessWindowFunction<TrafficSessionPageViewBean, TrafficSessionPageViewBean, Tuple3<String, String, String>, TimeWindow>.Context context, Iterable<TrafficSessionPageViewBean> elements, Collector<TrafficSessionPageViewBean> out) throws Exception {
                        String stt = DateFormatUtil.toYmdHms(context.window().getStart());
                        String edt = DateFormatUtil.toYmdHms(context.window().getEnd());
                        String date = DateFormatUtil.toDate(context.window().getStart());

                        TrafficSessionPageViewBean viewBean = elements.iterator().next();
                        viewBean.setStt(stt);
                        viewBean.setEdt(edt);
                        viewBean.setCurDate(date);

                        out.collect(viewBean);
                    }
                }
        );
        SingleOutputStreamOperator<TrafficSessionPageViewBean> joinDs = AsyncDataStream.unorderedWait(
                reduceDs,
                new MyAsyncFunction<TrafficSessionPageViewBean>("dim_base_source") {
                    @Override
                    public void join(JSONObject jsonObject, TrafficSessionPageViewBean orderBean) {
                        orderBean.setScName(jsonObject.getString("source_site"));
                    }

                    @Override
                    public String getKey(TrafficSessionPageViewBean orderBean) {
                        return orderBean.getSc();
                    }
                },
                60,
                TimeUnit.SECONDS
        );
        //写入到doris中
        joinDs.print(">>>");
        joinDs.addSink(new DorisSinkFunction<TrafficSessionPageViewBean>("dws_traffic_sc_is_new_sid_page_window"));

        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
